package com.baize.glinkad.utils;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Xml;

import com.baize.glinkad.utils.config.GlinkConfig;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;

public class ADTool {

    private static Bundle metaData;

    public static GlinkConfig loadPluginInfoByClass(Application application){
        String xmlPlugins = getAssetConfigs(application, "bz_plugin_config.xml");
        if (xmlPlugins == null){
            ADLogHelper.error( "fail to load plugin_config.xml");
            return null;
        }

        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(new StringReader(xmlPlugins));
            int eventType = parser.getEventType();
            while(eventType != XmlPullParser.END_DOCUMENT){

                switch(eventType){
                    case XmlPullParser.START_TAG:
                        String tag = parser.getName();
                        if("glinkplugin".equals(tag)){
                            GlinkConfig adPluginBean = new GlinkConfig();
                            adPluginBean.setClazz(parser.getAttributeValue(0));
                            adPluginBean.setDesc(parser.getAttributeValue(1));
                            adPluginBean.setAppId(parser.getAttributeValue(2));
                            adPluginBean.setAppKey(parser.getAttributeValue(3));
                            adPluginBean.setExt(parser.getAttributeValue(4));
                            ADLogHelper.info("loadPluginInfoByClass 加载配置:"+adPluginBean.toString());
                            return adPluginBean;
                        }
                }
                eventType = parser.next();
            }

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 获取assets目录下文件内容
     * @param context
     * @param assetsFile
     * @return
     */
    public static String getAssetConfigs(Context context, String assetsFile){
        InputStreamReader reader = null;
        BufferedReader br = null;
        try {
            reader = new InputStreamReader(context.getAssets().open(assetsFile));
            br = new BufferedReader(reader);

            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = br.readLine())!= null){
                sb.append(line);
            }

            return sb.toString();
        } catch (Exception e) {
            // TODO Auto-generated catch block
//			e.printStackTrace();
        }finally{
            if(br != null){
                try {
                    br.close();
                    br = null;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            if(reader != null){
                try {
                    reader.close();
                    reader = null;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

        return null;

    }

    public static String getCurrChannel(Context context){
        if(metaData == null){
            metaData = getMetaData(context);
        }
        return metaData.getString("BZHD_CHANNEL_GAME_NO");
    }

    private static Bundle getMetaData(Context context) {
        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if (appInfo != null && appInfo.metaData != null)
            {
                return appInfo.metaData;
            }
        } catch (PackageManager.NameNotFoundException e) {
        }

        return new Bundle();
    }

}
